package lk2014;

import java.awt.GridLayout;
import javax.swing.*;

public class Darstellungsformen {

	public static void adjazenzliste(Graph pGraph) {

		List lList = pGraph.getNodes();

		JFrame frame = new JFrame();
		frame.setTitle("Projekt");
		frame.setSize(1000, 800);

		JPanel panel = new JPanel();

		lList.toFirst();
		int x = 0;
		while (lList.hasAccess()) {
			x++;
			lList.next();
		}

		panel.setLayout(new GridLayout(2, x));

		lList.toFirst();
		for (int i = 0; i < x; i++) {
			JLabel label = new JLabel();
			label.setText(((GraphNode) lList.getObject()).getName());
			panel.add(label);
			lList.next();
		}

		lList.toFirst();
		for (int i = 0; i < x; i++) {
			List nList = pGraph.getNeighbours((GraphNode) lList.getObject());
			JTextArea textfeld = new JTextArea();
			String a = "";
			nList.toFirst();

			while (nList.hasAccess()) {
				GraphNode g = (GraphNode) nList.getObject();
				a = a
						+ g.getName()
						+ "("
						+ pGraph.getEdgeWeight((GraphNode) lList.getObject(), g)
						+ ")" + "\n";
				nList.next();
			}

			textfeld.setText(a);
			panel.add(textfeld);
			lList.next();
		}

		frame.getContentPane().add(panel);

		frame.setVisible(true);

	}

	public static void adjazensmatrix(Graph pGraph) {
		String[][] lMatrix;
		List lList = pGraph.getNodes();
		List lList1;
		int pAnzahl = 0;
		lList.toFirst();
		while (lList.hasAccess()) {
			pAnzahl++;
			lList.next();
		}
		// Matrix mit der passenden Größe wurde erstellt
		lMatrix = new String[pAnzahl + 1][pAnzahl + 1];
		lList.toFirst();
		int p = 1;
		// horizontal werden in der ersten Reihe die Namen der Knoten eingefügt
		while (lList.hasAccess()) {
			String lName = ((GraphNode) lList.getObject()).getName();
			lMatrix[0][p] = lName;
			p++;
			lList.next();
		}
		lList.toFirst();
		p = 1;
		// vertikal werden in der ersten Spalte die Namen der Knoten eingefügt
		while (lList.hasAccess()) {
			String lName = ((GraphNode) lList.getObject()).getName();
			lMatrix[p][0] = lName;
			p++;
			lList.next();
		}
		lList.toFirst();
		int q;

		// der Rest der Matrix soll ausgefüllt werden
		p = 1;
		while (lList.hasAccess()) {
			lMatrix[p][p] = "0";
			lList.toFirst();
			GraphNode lGraphNode1;
			lGraphNode1 = pGraph.getNode((String) ((GraphNode) lList
					.getObject()).getName());
			lList1 = pGraph.getNeighbours((GraphNode) lList.getObject());
			q = p + 1;
			lList.toFirst();
			lList.next();

			while (lList.hasAccess()) {

				if (Darstellungsformen
						.listeenthaelt((GraphNode) lList.getObject(), lList1)) {

					GraphNode lGraphNode2;
					lGraphNode2 = pGraph.getNode((String) ((GraphNode) lList1
							.getObject()).getName());
					double lGewicht = pGraph.getEdgeWeight(lGraphNode1,
							lGraphNode2);
					Integer lGew = (int) lGewicht;
					String lGewicht1 = lGew.toString();
					lMatrix[p][q] = lGewicht1;
					lMatrix[q][p] = lGewicht1;
				}

				else {
					lMatrix[p][q] = "nV";
					lMatrix[q][p] = "nV";
				}

				q++;
				lList.next();
			}

			p++;
			lList.toFirst();
			lList.remove();
		}

		// Ausgabe

		for (int i = 0; i < lMatrix.length; i++) {
			for (int j = 0; j < lMatrix.length; j++) {
				System.out.print("\t" + lMatrix[i][j]);
			}
			System.out.println();
		}

	}

	private static boolean listeenthaelt(GraphNode pNode, List pList) {

		pList.toFirst();
		while (pList.hasAccess()) {
			String lString1 = ((GraphNode) pList.getObject()).getName();
			if (lString1.equals(pNode.getName())) {
				return true;
			}
			pList.next();
		}
		return false;

	}
}
